package org.light.simpleauth;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Statement;
import org.light.domain.Util;
import org.light.utils.DomainUtil;
import org.light.utils.WriteableUtil;

public class LoginInterceptorGenerator extends Util{
	public LoginInterceptorGenerator(){
		super();
		super.fileName = "LoginInterceptor.go";
	}
	
	public LoginInterceptorGenerator(String packageToken){
		super();
		this.setPackageToken(packageToken);
		super.fileName = "LoginInterceptor.go";
	}

	@Override
	public String generateUtilString() {
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"package "+DomainUtil.getPackageTokenWithDot(this.getPackageToken())+"interceptor"));
		sList.add(new Statement(2000L,0,""));
		
		sList.add(new Statement(3000L,0,"import ("));
		sList.add(new Statement(5000L,1,"\"net/http\""));
		sList.add(new Statement(6000L,0,""));
		sList.add(new Statement(7000L,1,"session \"github.com/ScottHuangZL/gin-jwt-session\""));
		sList.add(new Statement(8000L,1,"\"github.com/gin-gonic/gin\""));
		sList.add(new Statement(9000L,0,")"));
		sList.add(new Statement(10000L,0,""));
		sList.add(new Statement(12000L,0,"func LoginInterceptor() gin.HandlerFunc {"));
		sList.add(new Statement(13000L,1,"return func(c *gin.Context) {"));
		sList.add(new Statement(20000L,2,"username, err := session.ValidateJWTToken(c)"));
		sList.add(new Statement(21000L,2,"loginFlag := false"));
		sList.add(new Statement(22000L,2,"if err == nil && username != \"\" {"));
		sList.add(new Statement(23000L,3,"loginFlag = true"));
		sList.add(new Statement(24000L,2,"}"));
		sList.add(new Statement(26000L,2,"if !loginFlag {"));
		
		sList.add(new Statement(26100L,3,"session.GetFlashes(c)"));
		sList.add(new Statement(26200L,3,"session.DeleteAllSession(c)"));
		
		sList.add(new Statement(27000L,3,"c.Redirect(http.StatusTemporaryRedirect, \"/\")"));
		sList.add(new Statement(28000L,3,"c.Abort()"));
		sList.add(new Statement(29000L,3,"return"));
		sList.add(new Statement(30000L,2,"}"));
		sList.add(new Statement(31000L,2,"c.Next()"));
		sList.add(new Statement(35000L,1,"}"));
		sList.add(new Statement(36000L,0,"}"));

		return WriteableUtil.merge(sList).getContent();
	}

}
